package com.yuque.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuque.dao.ArticleDao;
import com.yuque.dao.CategoryDao;
import com.yuque.dao.UserDao;
import com.yuque.pojo.article;
import com.yuque.pojo.category;
import com.yuque.util.QueryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private ArticleDao articleDao;
    @Override
    public Page<category> chaxun(QueryBean queryBean) {
        LambdaQueryWrapper<category> lambdaQueryWrapper =null;
        lambdaQueryWrapper =new  LambdaQueryWrapper<>();
         lambdaQueryWrapper.eq(category::getCreateUser, queryBean.getCreateUser());
        Page<category> pp = new Page<>(queryBean.getPageNo(),queryBean.getPageSize());
        categoryDao.selectPage(pp,lambdaQueryWrapper);

        return pp;
    }

    @Override
    @Transactional
    public void shanchu(Integer uid, Integer id) {
        LambdaQueryWrapper<category> lambdaQueryWrapper =null;
        lambdaQueryWrapper =new  LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category::getId, id);
        LambdaQueryWrapper<article> lambdaQueryWrapper1 =null;
        lambdaQueryWrapper1 =new  LambdaQueryWrapper<>();

        lambdaQueryWrapper1.eq(article::getCategoryId, id);
        lambdaQueryWrapper1.eq(article::getCreateUser,uid);

        articleDao.delete(lambdaQueryWrapper1);
        categoryDao.delete(lambdaQueryWrapper);
    }

    @Override
    public int selectByid(category category) {
        int i=0;
        if(category.getId()==null){
            i=   categoryDao.insert(category)  ;

        }else {
            i=    categoryDao.updateById(category);
        }

        return i;
    }

    @Override
    public category chaid(Integer id) {
        category category = categoryDao.selectById(id);
        return category;
    }

    @Override
    public List<category> chasuoyou() {
        List<category> categories = categoryDao.selectList(null);
        return categories;
    }


}

